from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.generics import CreateAPIView

# Create your views here.
from . import serializers
from .models import User


class UserView(CreateAPIView):
    """注册"""
    serializer_class = serializers.CreateUserSerializer




# url(r'^usernames/(?P<username>\w{5,20})/count/$', views.UsernameCountView.as_view()),
class UsernameCountView(APIView):
    """判断用户名是否重复注册"""
    def get(self,request,username):
        #用username查询用户记录是否存在
        count = User.objects.filter(username = username).count()

        #序列化
        data = {
            'username':username,
                'count':count
        }
        return Response(data)

# url(r'^mobiles/(?P<mobile>1[3-9]\d{9})/count/$', views.MobileCountView.as_view()),
class MobileCountView(APIView):
    """判断手机号是否重复注册"""
    def get(self,request,mobile):
        count = User.objects.filter(mobile=mobile).count()

        data = {
            'mobile': mobile,
            'count': count
        }
        return Response(data)